package com.nl.pulsardemo.pool.config.commons;

import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

import static com.nl.pulsardemo.pool.config.commons.Constants.SERVICE_URL;

/**
 * @author : ZhaoY
 * @description : PulsarClient单例
 * @date : 2021-01-31 15:00
 **/
@Slf4j
@Configuration
public class PulsarClientConfiguration {
    PulsarClient client;

    @Bean
    PulsarClient pulsarClient() throws PulsarClientException {
        client = PulsarClient.builder()
                .serviceUrl(SERVICE_URL)
                .build();
        log.info("注入 PulsarClient");
        return client;
    }

    @PreDestroy
    void close() {
        log.info("关闭 PulsarClient");
        client.closeAsync();

    }
}
